home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 7684 / 7684.xpi / resources / fmCommon.js next >
Text File  |  2009-11-20  |  9KB  |  260 lines

  1. /**
  2.  * Copyright (c) 2008, Jose Enrique Bolanos, Jorge Villalobos
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  *  * Redistributions of source code must retain the above copyright notice,
  9.  *    this list of conditions and the following disclaimer.
  10.  *  * Redistributions in binary form must reproduce the above copyright notice,
  11.  *    this list of conditions and the following disclaimer in the documentation
  12.  *    and/or other materials provided with the distribution.
  13.  *  * Neither the name of Jose Enrique Bolanos, Jorge Villalobos nor the names
  14.  *    of its contributors may be used to endorse or promote products derived
  15.  *    from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  21.  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  **/
  29.  
  30. var EXPORTED_SYMBOLS = [ "FireFM" ];
  31.  
  32. const Cc = Components.classes;
  33. const Ci = Components.interfaces;
  34.  
  35. Components.utils.import("resource://firefm/log4moz.js");
  36.  
  37. /**
  38.  * XXX: Add support for the JSON object in Firefox 3.0.*
  39.  * See https://developer.mozilla.org/En/Updating_extensions_for_Firefox_3.1#JSON
  40.  */
  41. if (typeof(JSON) == "undefined") {
  42.   Components.utils.import("resource://gre/modules/JSON.jsm");
  43.   JSON.parse = JSON.fromString;
  44.   JSON.stringify = JSON.toString;
  45.  
  46.   EXPORTED_SYMBOLS = [ "FireFM", "JSON" ];
  47. }
  48.  
  49. /**
  50.  * FireFM namespace.
  51.  */
  52. if (typeof(FireFM) == 'undefined') {
  53.   var FireFM = {
  54.     /* The FUEL Application object. */
  55.     get Application() { return this._application; },
  56.     /* The Fire.fm extension UUID */
  57.     get EXTENSION_UUID() { return "{6F0976E6-26F3-4AFE-BBEC-9E99E27E4DF3}"; },
  58.     /* The root branch for all Fire.FM preferences. */
  59.     get PREF_BRANCH() { return "extensions.firefm."; },
  60.     /* Platform constants */
  61.     get OS_WINDOWS()       { return 0; },
  62.     get OS_WINDOWS_VISTA() { return 1; },
  63.     get OS_MAC()           { return 2; },
  64.     get OS_LINUX()         { return 3; },
  65.     get OS_OTHER()         { return 4; },
  66.  
  67.     /* The logger for this object. */
  68.     _logger : null,
  69.     /* The FUEL Application object. */
  70.     _application : null,
  71.     /* Identifier for the operating system */
  72.     _os : null,
  73.     /* Reference to the observer service. We use this one a lot. */
  74.     obsService : null,
  75.     /* Flag used to control the chrome startup process. */
  76.     startupDone : false,
  77.     /* Overlay string bundle. */
  78.     overlayBundle : null,
  79.  
  80.     /**
  81.      * Initialize this object.
  82.      */
  83.     init : function() {
  84.       // Setup logging. See http://wiki.mozilla.org/Labs/JS_Modules.
  85.  
  86.       // The basic formatter will output lines like:
  87.       // DATE/TIME  LoggerName LEVEL  (log message)
  88.       let formatter = new Log4Moz.BasicFormatter();
  89.       let root = Log4Moz.repository.rootLogger;
  90.       let logFile = this.getFMDirectory();
  91.       let app;
  92.  
  93.       logFile.append("log.txt");
  94.  
  95.       // Loggers are hierarchical, lowering this log level will affect all
  96.       // output.
  97.       root.level = Log4Moz.Level["All"];
  98.  
  99.       // this appender will log to the file system.
  100.       app = new Log4Moz.RotatingFileAppender(logFile, formatter);
  101.       app.level = Log4Moz.Level["Warn"];
  102.       root.addAppender(app);
  103.  
  104.       // get a Logger specifically for this object.
  105.       this._logger = this.getLogger("FireFM");
  106.       this._logger.debug("init");
  107.  
  108.       this._application =
  109.         Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);
  110.       this.obsService =
  111.         Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  112.       this.overlayBundle =
  113.         Cc["@mozilla.org/intl/stringbundle;1"].
  114.           getService(Ci.nsIStringBundleService).
  115.             createBundle("chrome://firefm/locale/fmBrowserOverlay.properties");
  116.     },
  117.  
  118.     /**
  119.      * Creates a logger repository from Log4Moz.
  120.      * @param aName the name of the logger to create.
  121.      * @param aLevel (optional) the logger level.
  122.      * @return the created logger.
  123.      */
  124.     getLogger : function(aName, aLevel) {
  125.       let logger = Log4Moz.repository.getLogger(aName);
  126.  
  127.       logger.level = Log4Moz.Level[(aLevel ? aLevel : "All")];
  128.  
  129.       return logger;
  130.     },
  131.  
  132.     /**
  133.      * Gets a reference to the directory where Fire.fm will keep its files. The
  134.      * directory is created if it doesn't exist.
  135.      * @return reference (nsIFile) to the Fire.fm directory.
  136.      */
  137.     getFMDirectory : function() {
  138.       // XXX: there's no logging here because the logger initialization depends
  139.       // on this method.
  140.  
  141.       let directoryService =
  142.         Cc["@mozilla.org/file/directory_service;1"].
  143.           getService(Ci.nsIProperties);
  144.       let fmDir = directoryService.get("ProfD", Ci.nsIFile);
  145.  
  146.       fmDir.append("firefm");
  147.  
  148.       if (!fmDir.exists() || !fmDir.isDirectory()) {
  149.         // read and write permissions to owner and group, read-only for others.
  150.         fmDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0774);
  151.       }
  152.  
  153.       return fmDir;
  154.     },
  155.  
  156.     /**
  157.      * Encodes strings in the way Last.fm usually encodes them, which includes
  158.      * replacing space characters for + characters.
  159.      * @param aString the string to encode.
  160.      * @return the encoded string.
  161.      */
  162.     encodeFMString : function(aString) {
  163.       this._logger.debug("encodeFMString");
  164.  
  165.       if (null == aString) {
  166.         this._logger.error("encodeFMString. Invalid string.");
  167.         throw new Ce("Invalid string.");
  168.       }
  169.  
  170.       return encodeURIComponent(aString).replace(/\%20/g, "+");
  171.     },
  172.  
  173.     /**
  174.      * Decodes strings in the way Last.fm usually encodes them, which includes
  175.      * replacing space characters for + characters.
  176.      * @param aString the string to decode.
  177.      * @return the decoded string.
  178.      */
  179.     decodeFMString : function(aString) {
  180.       this._logger.debug("decodeFMString");
  181.  
  182.       if (null == aString) {
  183.         this._logger.error("decodeFMString. Invalid string.");
  184.         throw new Ce("Invalid string.");
  185.       }
  186.  
  187.       return decodeURIComponent(aString.replace(/\+/g, " "));
  188.     },
  189.  
  190.     /**
  191.      * Creates a nsIURI object from the given URL.
  192.      * @param aURL The URL used to create the nsIURI object.
  193.      * @return The nsIURI object if aURL is valid, otherwise null.
  194.      */
  195.     createURI : function(aURL) {
  196.       this._logger.debug("createURI");
  197.  
  198.       var uri = null;
  199.  
  200.       try {
  201.         uri =
  202.           Cc["@mozilla.org/network/io-service;1"].
  203.             getService(Ci.nsIIOService).newURI(aURL, null, null);
  204.       } catch (e) {
  205.         this._logger.debug("createURI. Error:\n" + e);
  206.       }
  207.  
  208.       return uri;
  209.     },
  210.  
  211.     /**
  212.      * Obtains an identifier for the operating system this extension is running
  213.      * on.
  214.      * @return One of the operating system constants defined in this object.
  215.      */
  216.     getOperatingSystem : function() {
  217.       this._logger.debug("getOperatingSystem");
  218.  
  219.       if (null == this._os) {
  220.         const REGEX_OS_WINDOWS = /^Win/i;
  221.         const REGEX_OS_MAC = /^Mac/i;
  222.         const REGEX_OS_LINUX = /^Linux/i;
  223.         const REGEX_OS_WINDOWS_VISTA = /Windows NT 6.0/i;
  224.  
  225.         let appShellService =
  226.           Cc["@mozilla.org/appshell/appShellService;1"].
  227.             getService(Ci.nsIAppShellService);
  228.         let navigator = appShellService.hiddenDOMWindow.navigator;
  229.         let platform = navigator.platform;
  230.  
  231.  
  232.         if (platform.match(REGEX_OS_MAC)) {
  233.           this._os = this.OS_MAC;
  234.         } else if (platform.match(REGEX_OS_WINDOWS)) {
  235.           let userAgent = navigator.userAgent;
  236.  
  237.           if (userAgent.match(REGEX_OS_WINDOWS_VISTA)) {
  238.             this._os = this.OS_WINDOWS_VISTA;
  239.           } else {
  240.             this._os = this.OS_WINDOWS;
  241.           }
  242.         } else if (platform.match(REGEX_OS_LINUX)) {
  243.           this._os = this.OS_LINUX;
  244.         } else {
  245.           this._os = this.OS_OTHER;
  246.         }
  247.       }
  248.  
  249.       return this._os;
  250.     }
  251.   };
  252. }
  253.  
  254. /**
  255.  * FireFM constructor. This sets up logging for the rest of the extension.
  256.  */
  257. (function() {
  258.   this.init();
  259. }).apply(FireFM);
  260.